﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;

namespace NetTopologySuite.Operation.Buffer
{
	internal class OffsetSegmentString
	{
		public OffsetSegmentString()
		{
			this._ptList = new List<Coordinate>();
		}

		public IPrecisionModel PrecisionModel
		{
			get
			{
				return this._precisionModel;
			}
			set
			{
				this._precisionModel = value;
			}
		}

		public double MinimumVertexDistance
		{
			get
			{
				return this._minimimVertexDistance;
			}
			set
			{
				this._minimimVertexDistance = value;
			}
		}

		public void AddPt(Coordinate pt)
		{
			Coordinate coordinate = new Coordinate(pt);
			this._precisionModel.MakePrecise(coordinate);
			if (this.IsRedundant(coordinate))
			{
				return;
			}
			this._ptList.Add(coordinate);
		}

		public void AddPts(Coordinate[] pt, bool isForward)
		{
			if (isForward)
			{
				for (int i = 0; i < pt.Length; i++)
				{
					this.AddPt(pt[i]);
				}
				return;
			}
			for (int j = pt.Length - 1; j >= 0; j--)
			{
				this.AddPt(pt[j]);
			}
		}

		private bool IsRedundant(Coordinate pt)
		{
			if (this._ptList.Count < 1)
			{
				return false;
			}
			Coordinate c = this._ptList[this._ptList.Count - 1];
			return pt.Distance(c) < this._minimimVertexDistance;
		}

		public void CloseRing()
		{
			if (this._ptList.Count < 1)
			{
				return;
			}
			Coordinate coordinate = new Coordinate(this._ptList[0]);
			Coordinate other = this._ptList[this._ptList.Count - 1];
			if (coordinate.Equals(other))
			{
				return;
			}
			this._ptList.Add(coordinate);
		}

		public void Reverse()
		{
		}

		public Coordinate[] GetCoordinates()
		{
			return this._ptList.ToArray();
		}

		public override string ToString()
		{
			return new GeometryFactory().CreateLineString(this.GetCoordinates()).ToString();
		}

		private readonly List<Coordinate> _ptList;

		private IPrecisionModel _precisionModel;

		private double _minimimVertexDistance;
	}
}
